package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SettingMapper;
import com.ruoyi.system.domain.Setting;
import com.ruoyi.system.service.ISettingService;

import javax.annotation.Resource;

/**
 * 程序设置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-06
 */
@Service
public class SettingServiceImpl implements ISettingService {
    @Resource
    private SettingMapper settingMapper;

    /**
     * 查询程序设置
     *
     * @param id 程序设置主键
     * @return 程序设置
     */
    @Override
    public Setting selectSettingById(Long id) {
        return settingMapper.selectSettingById(id);
    }

    /**
     * 查询程序设置列表
     *
     * @param setting 程序设置
     * @return 程序设置
     */
    @Override
    public List<Setting> selectSettingList(Setting setting) {
        return settingMapper.selectSettingList(setting);
    }

    /**
     * 新增程序设置
     *
     * @param setting 程序设置
     * @return 结果
     */
    @Override
    public int insertSetting(Setting setting) {
        setting.setCreateTime(DateUtils.getNowDate());

        int i = 0;

        List<Setting> settings = settingMapper.selectSettingList(new Setting());
        if (settings.isEmpty()) {
            i = settingMapper.insertSetting(setting);
        } else {
            Setting update = settings.get(0);
            update.setUpdateTime(DateUtils.getNowDate());
            update.setEndTime(setting.getEndTime());
            update.setSettleTime(setting.getSettleTime());
            update.setPriceUp(setting.getPriceUp());
            update.setDoseTime(setting.getDoseTime());
            update.setDoseFlag(setting.getDoseFlag());
            update.setContent(setting.getContent());
            i = settingMapper.updateSetting(update);
        }

        return i;
    }

    /**
     * 修改程序设置
     *
     * @param setting 程序设置
     * @return 结果
     */
    @Override
    public int updateSetting(Setting setting) {
        setting.setUpdateTime(DateUtils.getNowDate());
        return settingMapper.updateSetting(setting);
    }

    /**
     * 批量删除程序设置
     *
     * @param ids 需要删除的程序设置主键
     * @return 结果
     */
    @Override
    public int deleteSettingByIds(Long[] ids) {
        return settingMapper.deleteSettingByIds(ids);
    }

    /**
     * 删除程序设置信息
     *
     * @param id 程序设置主键
     * @return 结果
     */
    @Override
    public int deleteSettingById(Long id) {
        return settingMapper.deleteSettingById(id);
    }
}
